<template>
  <el-card class="attr attrList">
    <el-button :icon="Plus" type="primary" :disabled="!categoryStore.category3Id" @click="showSaveList">添加属性</el-button>
    <el-table :data="attrList" border>
      <el-table-column label="序号" width="60" type="index"></el-table-column>
      <el-table-column label="属性名称" prop="attrName"></el-table-column>
      <el-table-column label="属性列表">
        <template v-slot="{ row }">
          <el-tag v-for="attrList in row.attrValueList" :key="attrList.id" class="attr-tag">
            {{ attrList.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="{ row }">
        <el-button :icon="Edit" type="warning" />
        <el-tooltip content="删除属性" placement="bottom" effect="light">
          <div class="attr-btn">
            <el-popconfirm
                :title="`您确定要删除 ${row.attrName} 属性吗?`"
                @confirm="delAttr(row.id)"
              >
          
              <template #reference>
                <el-button :icon="Delete" type="danger"  />
              </template>
            </el-popconfirm>
          </div>
        </el-tooltip>
      </template>

      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import type { AttrList } from "@/api/product/model/attrModel";
import { useCategoryStore } from "@/stores/category";
import { watch, ref } from "vue";
import { ElMessage } from "element-plus";

const categoryStore = useCategoryStore();
const attrList = ref<AttrList>([]);

// 获取属性列表
const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;

  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};


watch(
  () => categoryStore.category3Id,
  async (category3Id) => {
    if (!category3Id) {
      attrList.value = [];
      return;
    }
    getAttrList();
  },
  { immediate: true }
);
const emit = defineEmits(["setIsShowAttrList","setAttr"]);
// 显示添加属性组件
const showSaveList = () => {
  emit("setIsShowAttrList", false);
};

//删除属性
const delAttr=async(id:number)=>{
 await delAttrApi(id)
 ElMessage.success('属性删除成功')
 getAttrList();
}
</script>

<style scoped>
.attrList {
  margin: 10px 0;
}
.attr-tag {
  margin: 5px;
}
.attr-btn{
  display: inline-block;
  margin-left: 10px;
}
</style>
